package com.action.wx.addressbook;

import com.action.wx.order.AddressVo;
import com.bean.Address_book;
import com.bean.Mb_account;
import com.bean.Member;
import com.bean.Tag;
import com.common.BaseAction;
import com.common.ModelAndJson;
import com.common.SystemConfig;
import com.service.addressbook.AddressBookService;
import com.service.tag.TagService;
import com.util.Config;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/9/14.
 */
@Controller
@RequestMapping("wx/addr")
public class AddressBookAction extends BaseAction{
    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private TagService tagService;

    @ResponseBody
    @RequestMapping("saveAddr")
    public String getProvinceOptions(ModelAndJson modelMap, Address_book ab) {
        if(ab.getId()==null){
            addressBookService.insert(ab);
        }else{
            addressBookService.updateByPrimaryKey(ab);
        }
        AddressVo addrvo = addressBookService.selectById(ab.getId());
        modelMap.put("addrvo",addrvo);
        modelMap.setTrue("");
        return modelMap.toString();
    }

    @ResponseBody
    @RequestMapping("delAddr")
    public String delAddr(ModelAndJson modelMap,Integer id) {
        addressBookService.deleteByPrimaryKey(id);
        modelMap.setTrue("");
        return modelMap.toString();
    }

    @ResponseBody
    @RequestMapping("setDefault")
    public String setDefault(ModelAndJson modelMap,Integer id) {
        Address_book defaultAdd = addressBookService.selectDefault();
        if(defaultAdd!=null){
            defaultAdd.setIsdefault((byte)0);
            addressBookService.updateByPrimaryKey(defaultAdd);
        }

        Address_book addr = addressBookService.selectByPrimaryKey(id);
        addr.setIsdefault((byte)1);
        addressBookService.updateByPrimaryKey(addr);
        modelMap.setTrue("");
        return modelMap.toString();
    }

    @ResponseBody
    @RequestMapping("sendHereSession")
    public String sendHereSession(ModelAndJson modelMap,Integer id) {
        AddressVo addrVo = addressBookService.selectById(id);
        this.getSession().setAttribute(SystemConfig.orderAddressSession,addrVo);
        modelMap.setTrue("");
        return modelMap.toString();
    }

    @RequestMapping("list")
    public ModelAndView list(Integer mid) {
        HashMap<String, Object> paraMap = new HashMap<>();
        HttpSession session = this.getSession();
        List<AddressVo> addrList = addressBookService.selectByMemberId(mid);
        List<Tag> tagList=tagService.findByParentId(0);
        paraMap.put("tagList",tagList);
        paraMap.put("mid",mid);
        paraMap.put("addrList",addrList);
        return new ModelAndView("/wx/addr/list", paraMap);
    }
}
